/**
 * @author Flower
 * @description Common Util
 */


export default {
    isObject: input => (typeof input === 'object') && !(input instanceof Array),
    /**
     * @param  input
     * @return true: input is not empty; false: input is empty
     * @description input != ('' || undefined || null)
     */
    notEmpty: function(input){
       return !!input;
    },
    /**
     * @param  input
     * @return input is an Array
     */
    isArray: function( input){
      return typeof input === 'object' && (input instanceof Array);
    },
    isEmptyObj:  input => {
        let isEmpty = true;
        Object.keys( input ).forEach( key =>{
          isEmpty = false;
        })
        return isEmpty;
    },
    encodeHtml : function(str){
      if(!str) return '';
      let arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
      return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,(all,t)=>arrEntities[t]);
    },
    /**
     * transfer '\n' to <br/>
     */
    toHtml : function(str){
      if(!str) return [];
      if ( !this.isArray(str) ) {
        var strArr = str.split('\n');
        return strArr;
      }else{
        return str;
      }
    },
    dateToUnix:function(date){
      var dateFullArr = date.split( ' ',2 ),
        dateArr = ( dateFullArr[0] ? dateFullArr[0] : '' ).split( '-',3 ),
        timeArr = ( dateFullArr[1] ? dateFullArr[1] : '' ).split( ':',3 );
      return (new Date(
          parseInt(dateArr[0], 10) || null,
          ( parseInt(dateArr[1], 10) || 1 ) - 1,
          parseInt(dateArr[2], 10) || null,
          parseInt(timeArr[0], 10) || null,
          parseInt(timeArr[1], 10) || null,
          parseInt(timeArr[2], 10) || null
        ).getTime()) / 1000;
    },
    deepCopy:function(obj, cache = []){
        // just return if obj is immutable value
        if (obj === null || typeof obj !== 'object') {
          return obj
        }

        // if obj is hit, it is in circular structure
        const hit = find(cache, c => c.original === obj)
        if (hit) {
          return hit.copy
        }

        const copy = Array.isArray(obj) ? [] : {}
        // put the copy into cache at first
        // because we want to refer it in recursive deepCopy
        cache.push({
          original: obj,
          copy
        })

        Object.keys(obj).forEach(key => {
          copy[key] = this.deepCopy(obj[key], cache)
        })

        return copy
      },
    /**
     * @usage
     * transfer unix to date format
     */
    unixToDate:function(timestamp,isTime){
      var time = new Date( timestamp * 1000),
          date = '',
          month = time.getMonth() + 1,
          day = time.getDate();
      if( time == 'Invalid Date') return timestamp;

      date += time.getFullYear() + "-";
      date += (month < 10 ?'0' + month : month) + "-";
      date += (day < 10 ?'0' + day : day) ;
      if(isTime){
        date +=" "+(time.getHours() < 10 ?'0' + time.getHours() : time.getHours()) + ":";
        date += (time.getUTCMinutes() < 10 ? '0' + time.getUTCMinutes() : time.getUTCMinutes()) + ":";
        date += time.getUTCSeconds() < 10 ? '0' + time.getUTCSeconds() : time.getUTCSeconds();
      }

      return date;
    },
    //时间戳转截止时间
    unixToEndTime:function(timestamp){
            var time = timestamp * 1000;
            var nowTime=new Date().getTime();
            if(time>=nowTime){
               var t=time-nowTime;
               if(t>86400000){
                  return  '剩余'+ Math.ceil(t/86400000) + '天';
               }else{
                  return  '剩余' + Math.ceil(t/3600000)+'小时';
               }
            }else{
              var t=nowTime-time;
               if(t>86400000){
                  return  '延期'+Math.ceil(t/86400000)+'天';
               }else{
                  return  '延期'+Math.ceil(t/3600000)+'小时';
               }
            }
    }
}


